﻿@using Moonglade.Core
@using Moonglade.DateTimeOps
@using Moonglade.Web.HtmlHelpers
@model IReadOnlyList<Moonglade.Model.PostCommentListItem>
@inject IDateTimeResolver DateTimeResolver

@{
    static string GetUserAvatarChar(string username)
    {
        if (string.IsNullOrWhiteSpace(username))
        {
            return "N/A";
        }

        var sets = username.Split(' ');
        if (sets.Length == 1)
        {
            return sets[0].First().ToString().ToUpper();
        }
        return sets.Length >= 2 ? $"{sets[0].First()}{sets[1].First()}".ToUpper() : "N/A";
    }
}

@if (null != Model && Model.Any())
{
    <div class="comment-list row">
        @foreach (var item in Model.OrderBy(m => m.CreateOnUtc))
        {
            <div class="col-md-1 col-sm-1 col-2 mb-3">
                @if (BlogConfig.ContentSettings.EnableGravatar)
                {
                    <div class="user-avatar">
                        @Html.GravatarImage(item.Email, 58, forceSecureRequest: true)
                    </div>
                }
                else
                {
                    <div class="user-avatar-char">
                        @GetUserAvatarChar(item.Username)
                    </div>
                }
            </div>
            <div class="col-md-11 col-sm-11 col-10 mb-3">
                <div class="card comment-item">
                    <div class="card-header">
                        <strong>@item.Username</strong>
                        <span class="text-muted float-right"> @DateTimeResolver.GetDateTimeWithUserTZone(item.CreateOnUtc).ToString("yyyy-M-d HH:mm")</span>
                    </div>
                    <div class="card-body">
                        @Html.Raw(Utils.ConvertMarkdownContent(item.CommentContent, Utils.MarkdownConvertType.Html))
                        <div>
                            @foreach (var reply in item.CommentReplies.OrderByDescending(t => t.ReplyTimeUtc))
                            {
                                <hr />
                                <div class="reply-container">
                                    <div>
                                        <strong>
                                            Blog Author Replied:
                                        </strong>
                                        <span class="float-right text-muted">Replied at @DateTimeResolver.GetDateTimeWithUserTZone(reply.ReplyTimeUtc).ToString("yyyy-M-d HH:mm")</span>
                                    </div>
                                    <p>
                                        @Html.Raw(Utils.ConvertMarkdownContent(reply.ReplyContent, Utils.MarkdownConvertType.Html))
                                    </p>
                                </div>
                            }
                        </div>
                    </div>
                </div>
            </div>
        }
    </div>
}
else
{
    <div class="alert alert-info">
        No Comments
    </div>
}